package com.wokeMy.view.channe.gaohuitong;

import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.MybankblindActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_bindcarlist;
import com.woke.data.UnionPayChannel;
import com.woke.method.MyApp;
import com.wokeMy.view.RecordCreditActivity;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;


public class GhtSelectActivity extends BaseActivity implements View.OnClickListener {

    TextView zfcard_ghtselect,jscard_ghtselect;
    TextView money_ghtselect_et;
    RelativeLayout zhifu_ghtselect_rl;
    RelativeLayout jieshuan_ghtselect_rl;
    ImageView back;
    TextView ghtselect_text_title;
    Button paybt;

    Data_bindcarlist listdata,payListdata;
    Intent intent;
    float money;
    int way_id,vip_level;
    String user_id,phone;
    String pay_card_no,card_no,expired_date,cvv,smsCode_bind;
    UnionPayChannel unionPayChannel;
    Dialog mydialog;

    Double yueMoney;
    String card;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_ght_select);
    }

    @Override
    public void initActivity() {
        zhifu_ghtselect_rl.setOnClickListener(this);
        jieshuan_ghtselect_rl.setOnClickListener(this);
        back.setOnClickListener(this);
        ghtselect_text_title.setOnClickListener(this);
        paybt.setOnClickListener(this);
        money_ghtselect_et.setText(money+" 元");

    }

    @Override
    public void initView() {
        money_ghtselect_et = myFindViewById(R.id.money_ghtselect_et);
        ghtselect_text_title = myFindViewById(R.id.ghtselect_text_title);

        zhifu_ghtselect_rl = myFindViewById(R.id.zhifu_ghtselect_rl);
        jieshuan_ghtselect_rl = myFindViewById(R.id.jieshuan_ghtselect_rl);
        zfcard_ghtselect = myFindViewById(R.id.zfcard_ghtselect);
        jscard_ghtselect = myFindViewById(R.id.jscard_ghtselect);
        back = myFindViewById(R.id.back_ghtselect_iv);
        paybt = myFindViewById(R.id.pay_ghtselect_bt);


    }

    @Override
    public void initData() {
        intent=getIntent();
        way_id = intent.getIntExtra ("way_id", 0);
        money = intent.getFloatExtra ("money", 0.01f);
        //        user_id = intent.getStringExtra("user_id");
        yueMoney=0.00;
        user_id=((MyApp)getApplication()).getDatas_load().getId();
        vip_level=(Integer.parseInt(((MyApp)getApplication()).getDatas_load().getUser_type()));
        //        unionPayChannel=(UnionPayChannel)intent.getSerializableExtra("unionPayChanne");
        //        Log.e("initData",money+" "+unionPayChannel.getId()+" "+unionPayChannel.get)
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_ghtselect_iv:
                finish();
                break;
            case R.id.ghtselect_text_title:
                Intent inte = new Intent (GhtSelectActivity.this, RecordCreditActivity.class);
                inte.putExtra ("way_id",way_id);
                startActivity(inte);
                break;
            case R.id.pay_ghtselect_bt:
                if (pay_card_no==null||pay_card_no.equals("")){
                    Toast.makeText(GhtSelectActivity.this,"付款未选择",Toast.LENGTH_SHORT).show();
                } else if(card_no==null||card_no.equals("")){
                    Toast.makeText(GhtSelectActivity.this,"收款卡未选择",Toast.LENGTH_SHORT).show();
                } else {
                    mydialog=  Util.createLoadingDialog(GhtSelectActivity.this);
                    yOrder();
                }
                //                Toast.makeText(GhtSelectActivity.this,"暂未开通",Toast.LENGTH_SHORT).show();
                break;
            case R.id.zhifu_ghtselect_rl: // 付款卡
                Intent intent = new Intent (GhtSelectActivity.this, MybankblindActivity.class);
                intent.putExtra ("czhiortxian", "zhifucard");
                startActivityForResult (intent, Constant.REQUEST_ZHIFU);
                break;
            case R.id.jieshuan_ghtselect_rl: // 收款卡
                Intent intent2 = new Intent (GhtSelectActivity.this, MybankblindActivity.class);
                intent2.putExtra ("czhiortxian", "wu");
                startActivityForResult (intent2, Constant.REQUEST_JIESHUAN);
                break;
            default:
                break;
        }
    }
    //高汇通快捷支付预下单
    private void  yOrder() {
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("card_no",pay_card_no);
        params.put("way",way_id);
        params.put("amount",money);
        params.put("other_card_no",card_no);
        Log.e("高汇通快捷支付预下单url",Constant.GHTYZF_URL);
        Log.e("高汇通快捷支付预下单params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.GHTYZF_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                //判断状态码
                if(i == 200){
                    //获取结果
                    try {
                        String result = new String(bytes, "utf-8");
                        //                        Toast.makeText(GhtSelectActivity.this, result, Toast.LENGTH_SHORT).show();
                        Log.e("高汇通快捷支付预下单Result", result);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            if (jsonObject.get("flag").equals("success")) {
                                JSONObject jsonObject1 = jsonObject.getJSONObject("data").getJSONObject("data");
                                String dealcode = jsonObject1.getString("dealcode");
                                if (dealcode.equals("10000")){
                                    String orderno=jsonObject1.getString("orderno");
                                    String needsms=jsonObject1.getString("needsms");
                                    Util.closeDialog(mydialog);
                                    if (needsms.equals("0")){
                                        tishi("抱歉，该通道暂不支持该银行卡");
//                                        Intent intent=new Intent(GhtSelectActivity.this,GhtZfActivity.class);
//                                        intent.putExtra("paylistdata",payListdata);
//                                        intent.putExtra("listdata",listdata);
//                                        intent.putExtra ("money",money);
//                                        intent.putExtra ("way_id",way_id);
//                                        intent.putExtra ("orderno",orderno);
//                                        startActivity(intent);
                                    } else if (needsms.equals("1")) {
                                        Intent intent=new Intent(GhtSelectActivity.this,GhtZfActivity.class);
                                        intent.putExtra("paylistdata",payListdata);
                                        intent.putExtra("listdata",listdata);
                                        intent.putExtra ("money",money);
                                        intent.putExtra ("way_id",way_id);
                                        intent.putExtra ("orderno",orderno);
                                        startActivity(intent);
//                                        zf(orderno);
                                    }
                                }else {
                                    tishi(jsonObject1.getString("dealmsg"));
                                }

                            } else {
                                Util.closeDialog(mydialog);
                                String info = jsonObject.getString("info");
                                if (jsonObject.has("data")){
                                    info=jsonObject.getJSONObject("data").getJSONObject("data").getString("dealmsg");
                                }
                                tishi(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(mydialog);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //        Log.e("RESPONSE_JIESHUAN",resultCode+"");
        if (resultCode==Constant.RESPONSE_ZHIFU){
            payListdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            pay_card_no=payListdata.cardno;
            zfcard_ghtselect.setText(pay_card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+pay_card_no.substring(pay_card_no.length()-3,pay_card_no.length()));
        }else if(resultCode==Constant.RESPONSE_JIESHUAN){
            listdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            card_no=listdata.cardno;
            jscard_ghtselect.setText(card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+card_no.substring(card_no.length()-3,card_no.length()));
        }
    }
    // 高汇通确认支付
    public void  zf(String orderno){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("card_no",pay_card_no); //付款卡
        params.put("orderNo",orderno);
        params.put("smsCode","");
        Log.e("高汇通确认支付url",Constant.GHTZF_URL);
        Log.e("高汇通确认支付params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.GHTZF_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                closeLoadDial(mydialog);
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("高汇通确认支付result", result);
                    try {
                        JSONObject jsonObject =new JSONObject(result);
                        String info = jsonObject.getString("info");
                        if (jsonObject.get("flag").equals("success")) {
                            if (jsonObject.has("data")){
                                info=jsonObject.getJSONObject("data").getJSONObject("data").getString("dealmsg");
                            }
                            tishi(info);
                        } else {

                            if (jsonObject.has("data")){
                                info=jsonObject.getJSONObject("data").getJSONObject("data").getString("dealmsg");
                            }
                            tishi(info);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                closeLoadDial(mydialog);
            }
        });
    }
}
